import 'package:demo/constants/sp_key.dart';
import 'package:demo/utils/gesture.dart';
import 'package:demo/utils/native/shared_preferences.dart';
import 'package:flutter/material.dart';

class FreshmanGuideUtils {
  static final PageController _pageController = PageController();

  /// 新人引导图
  static const Map<String, List<String>> _leadPicture = {
    "test": [
      "https://img0.baidu.com/it/u=3157367818,4061979966&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1083",
    ],
  };

  /// 是否显示新人引导图
  static Future<bool> isShowGuide(String key) async {
    final String keyName = "$LEAD_TIPS$key";
    return await SharedPreferencesUtils.getBool(keyName) != true;
  }

  /// 是否需要显示新手引导
  static Widget checkNeedShowLeadTipsDialog(BuildContext context, String path, VoidCallback onPopCallback) {
    if (_leadPicture.containsKey(path)) {
      final List<String> pictureList = _leadPicture[path]!;
      if (pictureList.isEmpty) return SizedBox.shrink();
      return PageView(
        controller: _pageController,
        physics: NeverScrollableScrollPhysics(),
        children: List.generate(
          pictureList.length,
          (index) {
            return GestureDetector(
              onTap: GestureUtils.throttle(
                () {
                  if (pictureList[index] == pictureList.last) {
                    final String keyName = "$LEAD_TIPS$path";
                    onPopCallback.call();
                    SharedPreferencesUtils.setBool(keyName, true);
                  } else {
                    _pageController.jumpToPage(index + 1);
                  }
                },
              ),
              child: pictureList[index].contains("http") || pictureList[index].contains("https")
                  ? Image.network(pictureList[index], fit: BoxFit.fill)
                  : Image.asset(pictureList[index], fit: BoxFit.fill),
            );
          },
        ),
      );
    }
    return SizedBox.shrink();
  }
}
